#include "ViewBalanceDlg.h"
#include "resource.h"
#include <iostream>
#include "MenuDlg.h"
#include "ValidateCardDlg.h"
#include "PrintDlg.h"
#include "common.h"
#include "AccountBussiness.h"

using namespace std;

LRESULT CALLBACK ViewBalanceDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{	
    ViewBalanceDlg *Obj;
    if (message==WM_INITDIALOG)
    {	
        Obj=(ViewBalanceDlg*)lParam;
        SetWindowLong(hWnd,GWL_USERDATA,(LONG)Obj);
    }
    Obj=(ViewBalanceDlg*)GetWindowLong(hWnd,GWL_USERDATA);
    if (Obj)
        return Obj->DialogProc(hWnd, message, wParam, lParam);
    return 0;
}

ViewBalanceDlg::ViewBalanceDlg(HINSTANCE hInstance, HWND hParent)
{
    DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG_VIEW_BALANCE), hParent, (DLGPROC)ViewBalanceDlgProc, (LPARAM)this);
}

ViewBalanceDlg::~ViewBalanceDlg(void)
{
    EndDialog(m_hDlg,0);
}

LRESULT ViewBalanceDlg::DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	char* pBalance;
    char sBalance[15];
    switch (message) 
    {
    case WM_INITDIALOG:
        {	
            AccountBussiness objAccBuss;
            m_hDlg = hWnd;		
			HWND hStaticAccountNo=GetDlgItem(m_hDlg,IDC_STATIC_ACCOUNT_NO);
			HWND hStaticCardNo=GetDlgItem(m_hDlg,IDC_STATIC_CARD_NO);
			HWND hStaticBalanceNo=GetDlgItem(m_hDlg,IDC_STATIC_BALANCE_NO);
			SetWindowText(hStaticAccountNo,TEXT(g_Account.GetAccountID()));
			SetWindowText(hStaticCardNo,TEXT(g_Card.GetCardID()));
            pBalance = objAccBuss.ConvertToString(g_Account.GetBalance());
			sprintf_s(sBalance, "%s VND", pBalance);
			SetWindowText(hStaticBalanceNo,sBalance);
            break;
        }		
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {	
        case IDC_BUTTON_CLOSE:
		{
			int MsgBoxID=MessageBox(m_hDlg,TEXT(PRINT_RECEIPT_TEXT),TEXT(PRINT_RECEIPT_CAPTION),MB_YESNO|MB_ICONQUESTION);
			if (MsgBoxID == IDYES)
			{
				EndDialog(m_hDlg,0);
				CPrintDlg objPrintDlg(hGlobalInst,m_hDlg);
				break;
			}
			else
            {
				MsgBoxID=MessageBox(m_hDlg,TEXT(CONTINOUS_TRANSACTION_TEXT),TEXT(CONTINOUS_TRANSACTION_CAPTION),MB_YESNO|MB_ICONQUESTION);
				if (MsgBoxID == IDYES)
				{
					EndDialog(m_hDlg,0);
					MenuDlg objMenuDlg(hGlobalInst,m_hDlg);						
					break;
				}
				else
                {		
					EndDialog(m_hDlg,0);
					CValidateCardDlg objValidateCard(hGlobalInst, NULL);						
					break;
				}
			}				
			return TRUE;
		}
        }  
    default:
        return FALSE;
    }

    return TRUE;
}